package com.tianyang.yukuaifuzs.utils.permissions;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_CONTACTS;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

/**
 * Description：权限校验
 */
public class PermissionsUtils {

    public static boolean checkReadStoragePermission(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            return true;
        }
        int readStoragePermissionState =
                ContextCompat.checkSelfPermission(activity, READ_EXTERNAL_STORAGE);

        boolean readStoragePermissionGranted = readStoragePermissionState == PackageManager.PERMISSION_GRANTED;

        if (!readStoragePermissionGranted) {
            ActivityCompat.requestPermissions(activity,
                    PermissionsConfig.PERMISSIONS_EXTERNAL_READ,
                    PermissionsConfig.REQUEST_EXTERNAL_READ);
        }
        return readStoragePermissionGranted;
    }

    public static boolean checkWriteStoragePermission(Fragment fragment) {
        int writeStoragePermissionState =
                ContextCompat.checkSelfPermission(fragment.getContext(), WRITE_EXTERNAL_STORAGE);

        boolean writeStoragePermissionGranted = writeStoragePermissionState == PackageManager.PERMISSION_GRANTED;

        if (!writeStoragePermissionGranted) {
            fragment.requestPermissions(PermissionsConfig.PERMISSIONS_EXTERNAL_WRITE,
                    PermissionsConfig.REQUEST_EXTERNAL_WRITE);
        }
        return writeStoragePermissionGranted;
    }

    public static boolean checkWriteStoragePermission(Context context) {

        int writeStoragePermissionState =
                ContextCompat.checkSelfPermission(context, WRITE_EXTERNAL_STORAGE);

        boolean writeStoragePermissionGranted = writeStoragePermissionState == PackageManager.PERMISSION_GRANTED;

        if (!writeStoragePermissionGranted) {
            if (context instanceof Activity) {
                ActivityCompat.requestPermissions((Activity) context, PermissionsConfig.PERMISSIONS_EXTERNAL_WRITE,
                        PermissionsConfig.REQUEST_EXTERNAL_WRITE);
            }
        }
        return writeStoragePermissionGranted;
    }

    public static boolean checkCameraPermission(Fragment fragment) {
        int cameraPermissionState = ContextCompat.checkSelfPermission(fragment.getContext(), CAMERA);

        boolean cameraPermissionGranted = cameraPermissionState == PackageManager.PERMISSION_GRANTED;

        if (!cameraPermissionGranted) {
            fragment.requestPermissions(PermissionsConfig.PERMISSIONS_CAMERA,
                    PermissionsConfig.REQUEST_CAMERA);
        }
        return cameraPermissionGranted;
    }

    public static boolean checkReadContactsPermission(Fragment fragment) {
        int cameraPermissionState = ContextCompat.checkSelfPermission(fragment.getContext(), READ_CONTACTS);

        boolean contactsPermissionGranted = cameraPermissionState == PackageManager.PERMISSION_GRANTED;

        if (!contactsPermissionGranted) {
            fragment.requestPermissions(PermissionsConfig.PERMISSIONS_READ_CONTACTS,
                    PermissionsConfig.REQUEST_READ_CONTACTS);
        }
        return contactsPermissionGranted;
    }
}
